<?php
session_start();
$num = 5;

$im         = imagecreatetruecolor(500, 500);
$background = imagecolorallocate($im, 250, 250, 250);
$red        = imagecolorallocate($im, 250, 0, 0);
$blue       = imagecolorallocate($im, 0, 0, 250);
$linecolor  = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

imagefill($im, 0, 0, $background);

for ($i = 0; $i < 200; $i++) {
    imagesetpixel($im, mt_rand(0, 500), mt_rand(0, 500), $red);
}
for ($i = 0; $i < 30; $i++) {

    imageline($im, mt_rand(0, 500), mt_rand(0, 500), mt_rand(0, 500), mt_rand(0, 500), $linecolor);
}
$allzimu = '0123456789qwertyuiopasdfghjklzxcvbnmZQWERSDFSAFGHRJJTFD';
$chang   = strlen($allzimu);
$index   = mt_rand(0, $chang - 1);
$str = '';
for ($i = 0; $i < $num; $i++) {
    $index     = mt_rand(0, $chang - 1);
    $zimu      = $allzimu[$index];
    $linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    $str .= $zimu;
    imagettftext($im, 100, mt_rand(-20, 20), 10 + 90 * $i, 280, $linecolor, "./comic.ttf", $zimu);
    // echo "第{$i}次循环的\$zimu是{$zimu} 此时的\$str是{$str}<br>";
}
$_SESSION['yzm'] = $str;
header('content-type:image/png');
imagepng($im);
imagedestroy($im);
